package controller;

import model.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IManagerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/7/21
 */
@Controller
@RequestMapping("manager")
public class ManagerController {
    @Autowired
    private IManagerService managerService;


    @RequestMapping("login")
    public String login(){
        return "manager/login";
    }
    //进行管理员登录的验证
    @RequestMapping("adminLoginCheck")
    @ResponseBody
    public String adminLoginCheck(Manager manager, HttpServletRequest req){
        Manager m = managerService.adminLoginCheck(manager);
        if (m == null) {
            return "0";
        }
        //创建session
        HttpSession session = req.getSession();
        session.setAttribute(m.getId().toString(), m);
        session.setMaxInactiveInterval(6000);//设置有效期60s
        return m.getId().toString();
    }

    //进行验证启动和禁用的信息
    @RequestMapping("loginAdmin")
    public ModelAndView mainPage(String id, HttpServletRequest req){
        //创建session
        HttpSession session = req.getSession();
        //根据id从session中获取管理员对象
        Manager m = (Manager) session.getAttribute(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("mName", m.getmName());
        mv.addObject("mLevel", m.getmLevel());
        mv.setViewName("manager/admin_index");
        return mv;
    }

    //查询所有的管理员的信息，超级管理员独门功能
    @RequestMapping("queryAllAdmin")
    public ModelAndView queryAllAdmin(Manager manager,Integer pageNo) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String,Object> map = managerService.queryAllAdmin(manager,pageNo);
        //将数据从map中取出来，传到前端
        modelAndView.addObject("list", map.get("list"));
        modelAndView.addObject("pageNo", map.get("pageNo"));
        modelAndView.addObject("totalPage", map.get("totalPage"));
        modelAndView.setViewName("manager/admin_manage");
        return modelAndView;
    }

    //跳转到添加管理员页面
    @RequestMapping("addManagerPage")
    public String addManagerPage(){
        return "manager/add_manager";
    }

    //添加一个管理员
    @RequestMapping("insertOneManager")
    @ResponseBody
    public String insertOneManager(Manager manager){
        return managerService.insertOneManager(manager);
    }

    //修改管理员的启用禁用状态   1-启用   0-禁用
    @RequestMapping("updateOneManager")
    @ResponseBody
    public String updateOneManager(Manager manager){
        return managerService.updateOneManager(manager);
    }
}
